<?php

class ShelterManagerCanEditAnimalAssertion implements Zend_Acl_Assert_Interface
{
    public function assert(Zend_Acl $acl,
                           Zend_Acl_Role_Interface $shelterManager = null,
                           Zend_Acl_Resource_Interface $animal = null,
                           $privilege = null)
    {
        if (!$shelterManager instanceof Application_Model_ShelterManager) {
            throw new Exception(__CLASS__
                              . '::'
                              . __METHOD__
                              . ' expects the role to be'
                              . ' an instance of ShelterManager');
        }
        
        if (!$animal instanceof Application_Model_Animal) {
            throw new Exception(__CLASS__
                              . '::'
                              . __METHOD__
                              . ' expects the role to be'
                              . ' an instance of Animal');
        }
        
        if ($shelterManager->id != null && $animal->shelterId == $shelterManager->id) {
            return true;
        } else {
            return false;
        }
    }
}

class ShelterManagerCanEditShelterAssertion implements Zend_Acl_Assert_Interface
{
    public function assert(Zend_Acl $acl,
                           Zend_Acl_Role_Interface $shelterManager = null,
                           Zend_Acl_Resource_Interface $shelter = null,
                           $privilege = null)
    {
        if (!$shelterManager instanceof Application_Model_ShelterManager) {
            throw new Exception(__CLASS__
                              . '::'
                              . __METHOD__
                              . ' expects the role to be'
                              . ' an instance of ShelterManager');
        }
        
        if (!$shelter instanceof Application_Model_Shelter) {
            throw new Exception(__CLASS__
                              . '::'
                              . __METHOD__
                              . ' expects the role to be'
                              . ' an instance of Shelter');
        }
        
        if ($shelterManager->id != null && $shelter->id == $shelterManager->id) {
            return true;
        } else {
            return false;
        }
    }
}

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{  
    protected function _initPlaceholders()
    {
        $this->bootstrap('view');
        $view = $this->getResource('view');
        $view->doctype('XHTML1_STRICT');
        
        // Set the initial title and separator:
        $view->headTitle('Save a Little Animal')
             ->setSeparator(' :: ');
    }
    
    protected function _initAcl()
    {
        $acl = new Zend_Acl();
        
        $acl->addRole(new Zend_Acl_Role('guest'));
        $acl->addRole(new Zend_Acl_Role('shelter_manager'), 'guest');
        $acl->addRole(new Zend_Acl_Role('administrator'));
        
        $acl->addResource(new Zend_Acl_Resource('animal'));
        $acl->addResource(new Zend_Acl_Resource('shelter'));
        
        // Guest may only view content
        $acl->allow('guest', null, 'view');
        
        // Shelter manager can edit own animals
        $acl->allow('shelter_manager', 'animal', 'edit', new ShelterManagerCanEditAnimalAssertion());
        
        // Shelter manager can edit own shelter (including adding animal to this shelter)
        $acl->allow('shelter_manager', 'shelter', 'edit', new ShelterManagerCanEditShelterAssertion());
        
        return $acl;
    }

}

